import { getStorage, setStorage } from '@/common/auth.js'
import { userLogin } from '@/common/api.js'
//#ifdef H5
const token =  uni.getStorageSync('token_key');//window.localStorage.getItem('pro_token') || '';
//#endif

//#ifdef MP-WEIXIN
const token = getStorage("token") || '';
//#endif
//#ifdef APP
const token = uni.getStorageSync('token_key');
//#endif

const state = {
    hasLogin: token ? true : false,  //是否登陆
    token: token, //toekn
	companyInfo: getStorage('company_key'),//选中品牌序号
	selIndex: getStorage('select_key'),  //选中品牌序号
	userInfo: getStorage('userInfo_key'), // 用户信息
	userSig: getStorage('userSig_key'), // message用户key
	contactList: getStorage('contactList_key'), // message用户key
	companyRole:getStorage('companyRole_key'), //获取公司权限
	userRole:getStorage('userRole_key'), //获取用户权限
    address: getStorage('address_key'), //首页选中的课程
}
console.log(123123,state.companyInfo)
const mutations = {
    SET_TOKEN(state, token) {
        state.token = token
        setStorage('token_key', token)
    },
	SET_COMPANY_INFO(state, company){ //公司信息
		state.companyInfo = company
		setStorage('company_key', company)
	},
	SET_SEL_INDEX(state, index){ //公司信息
		state.selIndex = index
		setStorage('select_key', index)
	},
	SET_USER_INFO(state, index) {
	    state.userInfo = index
	    setStorage('userInfo_key', index)
	},
	SET_USER_SIG(state, index) {
		state.userSig = index
		setStorage('userSig_key', index)
	},
	SET_CONTACT_LIST(state, contact) {
		state.contactList = contact
		setStorage('contactList_key', contact)
	},
	SET_COMPANY_ROLE(state, role) { //公司权限
		state.companyRole = role
		setStorage('companyRole_key', role)
	},
	SET_USER_ROLE(state, role) { //用户权限
		state.userRole = role
		setStorage('userRole_key', role)
	},
    SET_ADDRESS(state, address) {
        state.address = address
        setStorage('address_key', address)
    },
	
  
}
const actions = {
    setToken({ commit }, param) {
        commit('SET_TOKEN', param)
    },
    setUserAddress({ commit }, param) {
        commit('SET_USER_CITY', param)
    },
	setCompanyInfo({ commit }, param) { //公司信息
        commit('SET_COMPANY_INFO', param)
    },
	setSelIndex({ commit }, param) { //选中品牌序号
	    commit('SET_SEL_INDEX', param)
	},
	setUserInfo({ commit }, param) { //用户信息
	    commit('SET_USER_INFO', param)
	},
	setUserSig({ commit }, param) { //用户IM密钥
		commit('SET_USER_SIG', param)
	},
	setContactList({ commit }, param) { //通讯录
		commit('SET_CONTACT_LIST', param)
	},
	setCompanyRole({ commit }, param) { //公司权限
		commit('SET_COMPANY_ROLE', param)
	},
	setUserRole({ commit }, param) { //用户权限
		commit('SET_USER_ROLE', param)
	},
	setAddress({ commit }, param) { //设置用户的地址
	    commit('SET_ADDRESS', param)
	},
    //登陆
    login(state) {
        handleLogin(state);
    },
}
function handleLogin(state) {
    //#ifdef H5
    // h5登陆 -> 发送url给后台，后端把token 写在本地localStorage中
    console.log('login', state)
    const URI = encodeURIComponent(window.location.href);
    console.log(123123,URI)
    const share_key = window.localStorage.getItem('share_key')
    const os = window.localStorage.getItem('os')
    window.location.href = `${window.appConfig.apiUrl}User.login/login?os=${os}&re_uri=${URI}&from_type=1&login_type=8&share_key=axx`;
    state.hasLogin = true
    // handleAddcar()
    //#endif
    //#ifdef MP-WEIXIN
    uni.login({
        success(loginRes) {
            const { code } = loginRes
            const login_type = 8
            // userLogin({ code, login_type }).then(({data}) => {
            // 	const { token } = data;
            // 	state.token = token
            //     state.hasLogin = true
            //     uni.navigateTo({
            //         url: `/pages/public/tologin`
            //     });

            // })
            uni.request({
                url: 'http://edubak.jstxm.cn/wechat/User.login/login', //仅为示例，并非真实接口地址。
                data: {
                    login_type: login_type,
                    code: code,
                    share_key: '',
                    from_type:1
                },
                dataType: 'json',
                header: { 'content-type': 'application/x-www-form-urlencoded' },
                success: (res) => {
                    alert('授权')
                    console.log('res', res.data);
                    const token = res.data.data.token;
                    state.state.token = token
                    setStorage('token', token)
                    state.hasLogin = true
                    uni.navigateTo({
                        url: `/pages/public/tologin`
                    });
                    //this.text = 'request success';
                }
            });
        },
        fail(e) {
            console.log('errer->', e)
        }
    });
    //#endif
}
export default {
    namespaced: true,
    state,
    mutations,
    actions
}

